package com.designpatterns.observerpattern;

import java.util.ArrayList;
import java.util.List;

public class Subject
{
	private List<Observer> observers = new ArrayList<Observer>();
	private int state;

	public int getState()
	{
		return state;
	}

	public void setState(int state)
	{
		this.state = state;
		notifyAllObservers();
	}

	// 新闻添加观察者
	public void attach(Observer observer)
	{
		observers.add(observer);
	}

	// 新闻通知其他观察者
	public void notifyAllObservers()
	{
		for (Observer observer : observers)
		{
			observer.update();
		}
	}
}
